package com.hospital.wx.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.bean.BeanUtil;
import com.hospital.wx.common.CommonResult;
import com.hospital.wx.controller.form.SelectCanRegisterInDateRangeForm;
import com.hospital.wx.controller.form.SelectDoctorShiftDetailsBySubIdAndDateForm;
import com.hospital.wx.controller.form.SelectDoctorWorkPlanInTime;
import com.hospital.wx.exception.WxErrorCode;
import com.hospital.wx.service.DoctorWorkPlanService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/doctor/schedule")
@Tag(name = "DoctorScheduleController", description = "医生出诊信息接口管理")
@Slf4j
public class DoctorScheduleController {
    @Autowired
    private DoctorWorkPlanService doctorWorkPlanService;

    @PostMapping("/selectDoctorScheduleBySubIdAndDate")
//    @SaCheckLogin
    @Operation(summary = "查询医生排班信息")
    public CommonResult selectDoctorScheduleBySubIdAndDate(@RequestBody @Valid SelectCanRegisterInDateRangeForm form) {
        try {
            Map param = BeanUtil.beanToMap(form);
            ArrayList<String> list = doctorWorkPlanService.selectDoctorScheduleBySubIdAndDate(param);
            return CommonResult.ok().put(CommonResult.RETURN_RESULT, list);
        } catch (Exception e) {
            log.error("查询医生出诊信息过程中出现异常", e);
            return CommonResult.failed("查询医生出诊信息过程中出现异常");
        }
    }

    @PostMapping("/selectDoctorShiftDetailsBySubIdAndDate")
//    @SaCheckLogin
    @Operation(summary = "查询指定日期的出诊医生")
    public CommonResult selectDoctorShiftDetailsBySubIdAndDate(@RequestBody @Valid SelectDoctorShiftDetailsBySubIdAndDateForm form) {
        try {
            Map param = BeanUtil.beanToMap(form);
            ArrayList<HashMap> list = doctorWorkPlanService.selectDoctorShiftDetailsBySubIdAndDate(param);
            return CommonResult.ok().put(CommonResult.RETURN_RESULT, list);
        } catch (Exception e) {
            log.error("查询医生排班详情信息时出错，form: {}", form, e);
            // 返回失败信息
            return CommonResult.failed(WxErrorCode.DOCTOR_SCHEDULE_QUERY_FAILED);
        }
    }
}
